package es.udc.fic.hamNeggs.manager;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.udc.fic.hamNeggs.pojo.UserStories;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Repository(value = "userstories")
public class UserStoriesManager extends AbstractManager {
    private final static Logger LOG = Logger.getLogger(UserStoriesManager.class.getName());

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    public List<UserStories> getUserStoriesList() {
        List<UserStories> list = session.createQuery("from UserStories").list();

        Collections.sort(list,
                new Comparator<UserStories>() {
                    public int compare(UserStories o1, UserStories o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });

        return list;
    }

    @Transactional(readOnly = true)
    public UserStories getUserStories(Long id) {
        return (UserStories) session.get(UserStories.class, id);
    }

    @Transactional(readOnly = false)
    public void saveUserStories(UserStories userStories) {
        super.save(userStories, LOG);
    }

    @Transactional(readOnly = false)
    public void deleteUserStories(UserStories userStories) {
        super.delete(userStories, LOG);
    }

    @Transactional(readOnly = false)
    public void updateUserStories(UserStories userStories) {
        super.update(userStories, LOG);
    }
}
